package cn.tedu.smartmarketing.admin.service.impl;


import cn.tedu.smartmarketing.admin.mapper.AdminMapper;
import cn.tedu.smartmarketing.admin.pojo.dto.AdminCheck;
import cn.tedu.smartmarketing.admin.pojo.dto.AdminLoginParam;
import cn.tedu.smartmarketing.admin.pojo.dto.FinanceRecordParam;
import cn.tedu.smartmarketing.admin.pojo.entity.Checks;
import cn.tedu.smartmarketing.admin.pojo.entity.Finance;
import cn.tedu.smartmarketing.admin.pojo.vo.AdminVo;
import cn.tedu.smartmarketing.admin.pojo.vo.ChecksVO;
import cn.tedu.smartmarketing.admin.pojo.vo.FinanceListVO;
import cn.tedu.smartmarketing.admin.service.AdminService;
import cn.tedu.smartmarketing.base.exception.ServiceException;
import cn.tedu.smartmarketing.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public AdminVo login(AdminLoginParam adminLoginParam) {
       AdminVo adminVo=adminMapper.selectByUsername(adminLoginParam.getUsername());
       if (adminVo==null){
           throw new ServiceException(StatusCode.USERNAME_ERROR);
       }
       if (!adminVo.getPassword().equals(adminLoginParam.getPassword())){
           throw new ServiceException(StatusCode.PASSWORD_ERROR);
       }
        return adminVo;
    }

    @Override
    public void check(AdminCheck adminCheck) {
        log.debug("adminCheck:{}",adminCheck);
        Checks checks = new Checks();
        BeanUtils.copyProperties(adminCheck,checks);
           Short i= adminMapper.selectByid(checks.getCheckId());
           log.debug("checks:{}",checks.getCheckId());
           if (i==0){
               throw new ServiceException(StatusCode.OPERATION_SUCCESS);
           } else if (i==1) {
               throw new ServiceException(StatusCode.CHECK_NOTHROUGH);
           }else if (i==2) {
               throw new ServiceException(StatusCode.CHECK_THROUGH);
           }
    }

    @Override
    public void financeRecord(FinanceRecordParam financeRecordParam) {
        log.debug("交易信息业务：{}",financeRecordParam);
        Finance finance = new Finance();
        BeanUtils.copyProperties(financeRecordParam,finance);
        finance.setTime(new Date());
        adminMapper.financeRecord(finance);
    }

    @Override
    public List<FinanceListVO> financeList(Integer businessId) {
        log.debug("查询交易信息id业务:{}",businessId);
        List<FinanceListVO> list= adminMapper.financeList(businessId);
        log.debug("查询交易信息结果：{}",list);
        return list;
    }
    @Override
    public List<ChecksVO> listChecksRecord(String businessId) {
        log.debug("商家审核记录查询{}", businessId);
        List<ChecksVO> list = adminMapper.listChecksRecord(businessId);
        return list;
    }
}
